const db = require('../../mongoDB')

exports.auth = async (ctx)=> {
  const cookieUser = ctx.cookies.get('user')
  const cookieToken = ctx.cookies.get('token')
  let user = await db.user.findOne({ name: cookieUser })
  if (user && user.token == cookieToken ) {
    return true
  } else {
    ctx.cookies.set('user','',{maxAge:0})
    ctx.cookies.set('token', '', { maxAge: 0 })
    ctx.body = { code: 401,message:'登录状态异常，请重新登陆' }
    return false
  }
}

exports.ckconfig = {
  path:'/',       // 写cookie所在的路径
  maxAge: 1000*60*60*24,     // cookie有效时长
  //expires:new Date('2018-02-08'), // cookie失效时间
  httpOnly:false,       // 是否只用于http请求中获取
  overwrite:false      // 是否允许重写
}